package com.zhf.demo03.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/14 19:18
 */
@WebServlet(name = "CookieServlet3",urlPatterns = "/cookie3")
public class CookieServlet3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取客户端发来的cookie信息
        Cookie[] cookies = request.getCookies();
        System.out.println("获取到的cookie信息：");
        for (Cookie cookie:
             cookies) {
            //当获取到的cookie对象名字为name时，将对应的值修改为guanyu
            if (cookie.getName().equals("name")){
                cookie.setValue("guanyu");
                response.addCookie(cookie);
                break;
            }
        }
        System.out.println("cookie修改成功");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
